﻿using System.Drawing;
using System.Windows.Forms;
namespace RPG1
{
    public class RPGControls
    {
        private PictureBox picRole;
        private Label labelBottom;
        private Label labelTop;
        private PictureBox picWeapon;

        public RPGControls(PictureBox picRole, Label labelBottom, Label labelTop)
        {
            this.picRole = picRole;
            this.labelBottom = labelBottom;
            this.labelTop = labelTop;
        }

        public RPGControls(PictureBox picRole, Label labelBottom, Label labelTop, PictureBox picWeapon) {
            this.picRole = picRole;
            this.labelBottom = labelBottom;
            this.labelTop = labelTop;
            this.picWeapon = picWeapon;
        }

        public PictureBox PicRole { get => picRole; set => picRole = value; }
        public Label LabelBottom { get => labelBottom; set => labelBottom = value; }
        public Label LabelTop { get => labelTop; set => labelTop = value; }
        public PictureBox PicWeapon { get => picWeapon; set => picWeapon = value; }
        // 延时效果
        public void Delay(int times)
        {
            for (int i = 0; i < times; i++)
                Application.DoEvents();
        }
        // 初始化位置
        public void initLocation(Point location)
        {
            PicRole.Location = location;
            LabelBottom.Location = location;
            LabelTop.Location = location;
            LabelTop.Visible = true;
            LabelBottom.Visible = true;
            PicRole.Visible = true;
            if (PicWeapon != null) {
                PicWeapon.Location = new Point(location.X,location.Y-60);
                PicWeapon.Visible = true;
                PicWeapon.BringToFront();
            }
        }
        // 更新血量
        public void updateBlood(int currentLife, int originalLife )
        {
           LabelTop.Width = (int)(1.0 * currentLife / originalLife * LabelBottom.Width);
            LabelTop.Text = " " + (1.0 * currentLife / originalLife * 100) + "%";
            LabelBottom.BringToFront();
           LabelTop.BringToFront();
        }
        // 角色死亡
        public void died() {
            LabelTop.Visible = false;
            LabelBottom.Visible = false;
            PicRole.Visible = false;
            if (PicWeapon != null)
                PicWeapon.Visible = false;
        }

        // 返回原始位置
        public void goBack(Point originaLocation)
        {
            Delay(40000);
            if (PicWeapon != null) { // 法师，武器效果返回
                PicWeapon.Location = new Point(PicRole.Location.X + PicRole.Width / 2, PicRole.Location.Y - PicWeapon.Height - 15);
                PicWeapon.BringToFront();
            } else {    // 战士，人移动返回
                int diffX = originaLocation.X - PicRole.Location.X;
                int diffY = originaLocation.Y - PicRole.Location.Y;
                for (int i = 1; i <= 10; i++)
                {
                    PicRole.Location = new Point(PicRole.Location.X + diffX / 10,
                        PicRole.Location.Y + diffY / 10);
                    LabelTop.Location = PicRole.Location;
                    LabelBottom.Location = PicRole.Location;
                    Delay(20000);
                }
            }
        }
        // 移动
        public void move(int diffX, int diffY) {
            if (PicWeapon != null) { // 法师，魔法效果移动攻击
                for (int i = 1; i <= 10; i++) {
                    PicWeapon.Location = new Point(PicWeapon.Location.X + diffX / 10, PicWeapon.Location.Y + diffY / 10);
                    Delay(20000);
                }
            } else {    // 战士，人体移动攻击
                for (int i = 1; i <= 10; i++) {
                    PicRole.Location = new Point(PicRole.Location.X + diffX / 10, PicRole.Location.Y + diffY / 10);
                    LabelBottom.Location = PicRole.Location;
                    LabelTop.Location = PicRole.Location;
                    Delay(20000);
                }
            }
        }
    }
}
